//
//  WBWelcomeView.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/10/14.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit
import SDWebImage

//欢迎视图
class WBWelcomeView: UIView {

    
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var tipView: UILabel!
    
    @IBOutlet weak var bottomCons: NSLayoutConstraint!
    
    class func welcomeView() -> WBWelcomeView {
        
        let nib = UINib.init(nibName: "WBWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    
    //只是刚刚从xib二进制数据加载完成，还没有和代码的连线建立起关系，不要在这个方法处理ui
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        print("initWithCoder===\(iconView)")
        
    }
    
    
    
    override func awakeFromNib() {
        print("awakeFromNib===\(iconView)")
        
        guard let urlString = WBNetworkingManager.shared.userAccount.avatar_large,
            let url = URL(string: urlString) else {
            
            return
        }
        
        iconView.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
    }
    
    
    //自动布局系统更新完成后，会自动调用此方法，通常是对子视图进行修改
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    
    //view  显示在屏幕上的时候会掉用此方法
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        self.layoutIfNeeded()

        bottomCons.constant = bounds.height - 200
        
        UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
            self.layoutIfNeeded()
            
            }) { (_) in
                UIView.animate(withDuration: 1.0, animations: { 
                    self.tipView.alpha = 1.0
                    }, completion: { (_) in
                        
                        self.removeFromSuperview()
                        
                })
        }
    }
}
